package com.egaleye.core.log;

import com.egaleye.core.context.HttpRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import java.util.Map;

/**
 *  tangkaijun
 */
public class LogHttpRequestContextAdvice implements MethodBeforeAdvice {

    private static final Logger logger= LoggerFactory.getLogger(LogHttpRequestContextAdvice.class);

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        if(!logger.isDebugEnabled()){
            return;
        }

        StringBuilder sb=new StringBuilder();

        Map<String, Object> ctxMap= HttpRequestContext.getRequestContext();
        if(ctxMap!=null){
            for(Map.Entry<String,Object> entry:ctxMap.entrySet()){
                if(!"Stopwatch".equals(entry.getKey())){
                    sb.append(entry.getKey()).append("  ").append(entry.getValue()).append("\r\n");
                }
            }
        }

        logger.debug("开始执行方法 -> {}", method.toGenericString());
        logger.debug(">>>>>>>>>>>>>> 开始输出web 请求信息 >>>>>>>>>>>>>>\r\n{}\r\n", sb.toString());
        logger.debug("<<<<<<<<<<<<<< 结束输出web 请求信息 <<<<<<<<<<<<<<");
    }
}
